package com.tg.app.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.tg.app.R;

public class SDCardTipView extends LinearLayout {
    private TextView mTextView;
    private LinearLayout mLinearLayout;
    private ImageView mImageView;

    public SDCardTipView(Context context) {
        super(context);
        initContentView(context);
    }

    public SDCardTipView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SDCardTipView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initContentView(context);
    }


    private void initContentView(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.layout_sdcard_tip, this, true);
        mTextView = rootView.findViewById(R.id.text_sdcard_info);
        mLinearLayout = rootView.findViewById(R.id.ll_bg);
        mImageView = rootView.findViewById(R.id.image_flag);
    }

    public void setText(final int resid){
        mTextView.setText(resid);
    }

    public void setBackground(@DrawableRes int resid){
        mLinearLayout.setBackgroundResource(resid);
    }

    public void setGif(Context context, Integer resourceId ){
        if (context instanceof Activity) {
            Activity activity = (Activity) context;
            if (activity.isFinishing() || activity.isDestroyed()) {
                return;
            }
        }
        try {
            Glide.with(context).asGif().load(resourceId).into(mImageView);
        } catch (Throwable ex) {
            // ignore
        }

    }
}
